var add_all = allarea()
var city = [];
var county = [];
//所在地址
$('.province').html(getadd(add_all))
city = add_all[0].city
$('.city').html(getadd(city))
county = add_all[0].city[0].district
$('.county').html(getadd(county))
form.render('select');
//选择省
form.on('select(province)', function(data) {
	console.log(getdetail(data.value, add_all, 'id'))
	city = getdetail(data.value, add_all, 'id').city
	$('.city').html(getadd(city))
	county = getdetail(data.value, add_all, 'id').city[0].district
	$('.county').html(getadd(county))

	form.render('select');
});
//选择县
form.on('select(city)', function(data) {
	county = getdetail(data.value, city, 'id').district
	$('.county').html(getadd(county))

	form.render('select');
});
$('.company_name').on('click',function(){
	opsearch('{:url("html/system/company")}?op=search')
})
$('.username').on('click',function(){
	opsearch('{:url("html/system/user_add")}?op=search&role_id={$setting.role_passenger}')
})
{if $Think.get.passenger_id}
//请求数据
ajax(api.passenger.list,{
	where_json:jsonchange({
		passenger_id:['in','{$Think.get.passenger_id}']
	})
	},'post').done(function(res1){
	
		for(var key in res1.data[0]){
			if(key=='sex'||key=='is_check'||key=='is_vip'||key=='is_instead'){
			
				$(`[name=${key}]`).each(function(){
					
					if($(this).val()==res1.data[0][key]){
						$(this).attr('checked',true)
					}
				})
			}else if(key=='city'){
				if(res1.data[0][key]){
					if(res1.data[0].province>0){
						city = getdetail(res1.data[0].province, add_all, 'id').city
						$('.city').html(getadd(city))
						$('select[name='+key+']').val(res1.data[0][key])
					}
					
				}
			}else if(key=='county'){
				if(res1.data[0][key]){
					if(res1.data[0].province>0||res1.data[0].city>0){
					city = getdetail(res1.data[0].province, add_all, 'id').city
					county = getdetail(res1.data[0].city, city, 'id').district
					}
					$('.county').html(getadd(county))
					$('select[name='+key+']').val(res1.data[0][key])
				}
//				
			}else if(key=='source'){
				if(res1.data[0][key]){
					console.log('source')
					console.log(res1.data[0][key])
					$('select[name='+key+']').val(res1.data[0][key])
				}
//				
			}else{
				$('input[name='+key+']').val(res1.data[0][key])
				$('select[name='+key+']').val(res1.data[0][key])
				if(key=='head_portrait'){
					console.log('head_portrait')
					console.log(res1.data[0][key])
					$(`.head_portrait`).attr('src',res1.data[0][key])
				}
//				
			}
			
		}
		form.render();
{/if}




//请求公司
	Load()
	ajax(api.company.list,{page:1,pagenum:9999},'post').done(function(res){
		
		Load(1)
		var options2 = `<option value='0'>请选择公司</option>`;
		for(var i = 0; i < res.data.length; i++) {
			{if $Think.get.passenger_id}
			if(res.data[i].company_id==res1.data[0].company_id){
				options2 += `<option selected value='${res.data[i].company_id}'>${res.data[i].company_name}</option>`
			}else{
				options2 += `<option value='${res.data[i].company_id}'>${res.data[i].company_name}</option>`
			}
			{else/}
			options2 += `<option value='${res.data[i].company_id}'>${res.data[i].company_name}</option>`
			{/if}
			
		}
		$('.company_id').html(options2)
		form.render('select');
		form.on('select(company_id)', function(data) {
		
	//		form.render('select');
		});
		
	}).fail(function(){
		
	})
{if $Think.get.passenger_id}
}).fail(function(){
	Err('请求失败')
})
{/if}



//请求用户
function ajaxuser(value){
	var where_json={
		username:['like','%%']
	};
	
	ajax(api.user.list,{page:1,pagenum:20,where_json:jsonchange(where_json)},'post').done(function(res){
		
		Load(1)
		var options2 = `<option value='0'>请选择用户</option>`;
	//	for(var i = 0; i < res.data.length; i++) {
	//		options2 += `<option value='${res.data[i].user_id}'>${res.data[i].username}</option>`
	//	}
	//	$('.user_id').html(options2)
	//	form.render('select');
		
	}).fail(function(){
		
	})
}
$('.city').on('change', function() {})

function getadd(arr) {
	var options = '';
	for(var i = 0; i < arr.length; i++) {
		options += `<option value='${arr[i].id}'>${arr[i].name}</option>`
	}
	return options
}
//出生年月
laydate.render({
	elem: '#birthday' //指定元素
});

var data = {}
upImage(api.upimage.upimage, '#test1', '#img1', data,'head_portrait')

form.on('submit(ss1)', function(data1) {
	$('input[name="head_portrait"]').val(data.head_portrait)
	console.log(data1.elem) //被执行事件的元素DOM对象，一般为button对象
	console.log(data1.form) //被执行提交的form对象，一般在存在form标签时才会返回
	console.log(data1.field) //当前容器的全部表单字段，名值对形式：{name: value}
	console.log($('input[name="sex"]:checked').val())
	{if $Think.get.passenger_id}
	data1.field.passenger_id='{$Think.get.passenger_id}'
	sumbit=api.passenger.edit
	{else/}
	sumbit=api.passenger.add
	{/if}
	for(var key in data){
		data1.field[key]=data[key]
	}
	ajax(sumbit, data1.field, 'post').done(function(res) {
		if(res.code==1){
			
			{if $Think.get.passenger_id}
			gototitle('编辑完成',window.location.href)
			{else/}
			gototitle('添加完成','{:url("/html/system/passenger")}')
			{/if}
			
			
		}else{
			Err(res.msg)
		}
	}).fail(function(err) {
		Err('请求失败')
	});

	return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
});